<style lang="less">
    @import "./main.less";
    .logo-bar{
        position: fixed;
        padding: 8px 12px;
        text-align: center;
        top: 0;
        left: 0px;
        z-index: 9999;
        background-color: #495060;
        color: #fff;
        font-size: 20px;
        img{
            height: 36px;
            width: auto;
        }
    }
</style>
<template>
    <div class="main" :class="{'main-hide-text': shrink}">
        <div class="sidebar-menu-con" :style="{width: shrink?'60px':'200px', overflow: shrink ? 'visible' : 'auto'}">
            <shrinkable-menu
                    :shrink="shrink"
                    @on-change="handleSubmenuChange"
                    :theme="menuTheme"
                    :before-push="beforePush"
                    :open-names="openedSubmenuArr"
                    :menu-list="getAddRouterList">
                <div slot="top" class="logo-bar">
                   <div v-show="!shrink" class="flex-left">
                       <div style="height: 50px;display: flex;justify-content: center;align-items: center;">
                           <img src="../images/login-logo.png" key="min-logo"/>
                       </div>
                       <p style="line-height: 50px;font-weight: bold;margin-left: 10px;">智造管理平台</p>
                   </div>
                   <img v-show="shrink" src="../images/login-logo.png" key="min-logo" style="width: 36px;"/>
               </div>
                <!--<div slot="top" class="logo-con">
                    <img v-show="!shrink" style="padding-right: 8px;" src="../images/menu-logo.png" key="max-logo" />
                    <img v-show="shrink" src="../images/menu-logo-min.png" key="min-logo" />
                </div>-->
            </shrinkable-menu>
        </div>
        <div class="main-header-con" :style="{paddingLeft: shrink?'60px':'200px'}">
            <div class="main-header">
                <div class="navicon-con">
                    <Button :style="{transform: 'rotateZ(' + (this.shrink ? '-90' : '0') + 'deg)'}" type="text" @click="toggleClick">
                        <Icon type="md-menu" size="32"></Icon>
                    </Button>
                </div>
                <div class="header-middle-con">
                    <div class="main-breadcrumb">
                        <breadcrumb-nav :currentPath="currentPath"></breadcrumb-nav>
                    </div>
                </div>
                <div class="header-avator-con">
                    <full-screen v-model="isFullScreen" @on-change="fullscreenChange"></full-screen>
                    <lock-screen></lock-screen>
                    <message-tip v-model="mesCount"></message-tip>
                    <!--<theme-switch></theme-switch>-->
                    <div class="user-dropdown-menu-con" style="width: 155px;">
                        <Row type="flex" justify="end" align="middle" class="user-dropdown-innercon">
                            <Dropdown transfer trigger="click" @on-click="handleClickUserDropdown">
                                <a href="javascript:void(0)">
                                    <span class="main-user-name">{{ userName }}</span>
                                    <Icon type="md-arrow-dropdown" />
                                </a>
                                <DropdownMenu slot="list">
                                    <DropdownItem name="ownSpace">个人中心</DropdownItem>
                                    <DropdownItem name="loginout" divided>退出登录</DropdownItem>
                                </DropdownMenu>
                            </Dropdown>
                            <Avatar :src="avatorPath" style="background: #619fe7;margin-left: 10px;"></Avatar>
                        </Row>
                    </div>
                </div>
            </div>
            <div class="tags-con">
                <tags-page-opened :pageTagsActiveName="pageTagsActiveName" :pageTagsList="pageTagsList"></tags-page-opened>
            </div>
        </div>
        <div class="single-page-con" :style="{left: shrink?'60px':'200px'}">
            <div class="single-page">
                <loading v-show="getLoading"></loading>
                <error-modal
                        :modal-state="getErrorStatus.bErrorStatus"
                        :tip-msg="getErrorStatus.bErrorMsg"
                        :status="getErrorStatus.status"
                ></error-modal>
                <!--<keep-alive :include="cachePage">
                    <router-view></router-view>
                </keep-alive>-->
                <!--缓存的视图-->
                <keep-alive>
                    <router-view v-if="$route.meta.keepAlive" style="position: relative"></router-view>
                </keep-alive>
                <!--不缓存的视图-->
                <router-view v-if="!$route.meta.keepAlive" style="position: relative">
                </router-view>
            </div>
        </div>
    </div>
</template>
<script>
    import shrinkableMenu from './main-components/shrinkable-menu/shrinkable-menu.vue';
    import tagsPageOpened from './main-components/tags-page-opened.vue';
    import breadcrumbNav from './main-components/breadcrumb-nav.vue';
    import fullScreen from './main-components/fullscreen.vue';
    import lockScreen from './main-components/lockscreen/lockscreen.vue';
    import messageTip from './main-components/message-tip.vue';
    import themeSwitch from './main-components/theme-switch/theme-switch.vue';
    import Cookies from 'js-cookie';
    import util from '@/libs/util.js';
    import loading from '../loading/loading';
    import { mapGetters } from 'vuex';
    import errorModal from './components/error-modal/errorModal';
    import iView from 'iview';
    export default {
        components: {
            shrinkableMenu,
            tagsPageOpened,
            breadcrumbNav,
            fullScreen,
            lockScreen,
            messageTip,
            themeSwitch,
            loading,
            errorModal
        },
        data () {
            return {
                moduleAllList: [],
                menuOpenShow: false,
                menuList: [],
                shrink: false,
                userName: '',
                isFullScreen: false,
                openedSubmenuArr: this.$store.state.app.openedSubmenuArr,
                pageTagsActiveName: ''
            };
        },
        computed: {
            ...mapGetters([
                'getLoading', 'getOrder', 'getErrorStatus', 'returnErrorMsg', 'getAddRouterList'
            ]),
            pageTagsList () {
                return this.$store.state.app.pageOpenedList; // 打开的页面的页面对象
            },
            currentPath () {
                return this.$store.state.app.currentPath; // 当前面包屑数组
            },
            avatorPath () {
                return localStorage.avatorImgPath;
            },
            cachePage () {
                return this.$store.state.app.cachePage;
            },
            lang () {
                return this.$store.state.app.lang;
            },
            menuTheme () {
                return this.$store.state.app.menuTheme;
            },
            mesCount () {
                return this.$store.state.app.messageCount;
            }
        },
        methods: {
            init () {
                let pathArr = util.setCurrentPath(this, this.$route.name);
                this.$store.commit('updateMenulist');
                if (pathArr.length >= 2) {
                    this.$store.commit('addOpenSubmenu', pathArr[1].name);
                }
                this.$api.common.userInfoRequest().then(res => {
                    if (res.data.status === 200) this.userName = res.data.res.name;
                });
                let messageCount = 3;
                this.messageCount = messageCount.toString();
                this.checkTag(this.$route.name);
                this.$store.commit('setMessageCount', 3);
            },
            toggleClick () {
                this.shrink = !this.shrink;
            },
            handleClickUserDropdown (name) {
                if (name === 'ownSpace') {
                    util.openNewPage(this, 'ownspace_index');
                    this.$router.push({
                        path: 'ownspace',
                        query: {},
                        params: {}
                    });
                } else if (name === 'loginout') {
                    // 退出登录
                    Cookies.remove('token');
                    Cookies.remove('routeName');
                    localStorage.removeItem('serviceList');
                    localStorage.removeItem('cachePage');
                    localStorage.removeItem('pageOpenedList');
                    this.$store.commit('logout', this);
                    this.$store.commit('clearOpenedSubmenu');
                    this.$store.state.addRouterList = [];
                    this.$store.state.isLoadModule = false;
                    this.$router.push({
                        path: 'login',
                        query: {},
                        params: {}
                    });
                    iView.LoadingBar.finish();
                }
            },
            checkTag (name) {
                let openpageHasTag = this.pageTagsList.some(item => {
                    if (item.name === name) {
                        return true;
                    }
                });
                if (!openpageHasTag) { //  解决关闭当前标签后再点击回退按钮会退到当前页时没有标签的问题
                    util.openNewPage(this, name, this.$route.params || {}, this.$route.query || {});
                }
            },
            handleSubmenuChange (selectRouterName) {
            },
            beforePush (name) {
                // if (name === 'accesstest_index') {
                //     return false;
                // } else {
                //     return true;
                // }
                return true;
            },
            fullscreenChange (isFullScreen) {},
            getTrees (list, parentId) {
                let items = {};
                // 获取每个节点的直属子节点，*记住是直属，不是所有子节点
                for (let i = 0; i < list.length; i++) {
                    let key = list[i].parentId;
                    if (items[key]) {
                        items[key].push(list[i]);
                    } else {
                        items[key] = [];
                        items[key].push(list[i]);
                    }
                }
                return this.formatTree(items, parentId);
            },
            formatTree (items, parentId) {
                let result = [];
                if (!items[parentId]) {
                    return result;
                }
                for (let t of items[parentId]) {
                    t.children = this.formatTree(items, t.id);
                    if (t.name.indexOf('/') !== -1) { // 路由存在"/"的在前面拼接'dynamic-router'
                        t.name = 'dynamic-router' + t.name.substr(t.name.indexOf('/'));
                        t.path = 'dynamic-router' + t.path.substr(t.path.indexOf('/'));
                    };
                    result.push(t);
                }
                return result;
            }
        },
        watch: {
            '$route' (to) {
                if (to.name === 'dynamic-router') {
                    // this.pageTagsActiveName = to.path.substr(1);
                    this.pageTagsActiveName = `${to.name}/${to.params.aId}`;
                } else {
                    this.pageTagsActiveName = to.name;
                };
                if (to.name === 'home_index') {
                    Cookies.set('moduleCode', '');
                }
                this.$store.commit('setCurrentPageName', to.name);
                // Cookies.set('routeName', to.name);
                let pathArr = util.setCurrentPath(this, to.name);
                if (pathArr.length > 1) {
                    this.$store.commit('addOpenSubmenu', pathArr[1].name);
                    // Cookies.set('addOpenSubmenu', pathArr[1].name);
                }
                this.checkTag(to.name);
                localStorage.currentPageName = to.name;
            },
            lang () {
                util.setCurrentPath(this, this.$route.name); // 在切换语言时用于刷新面包屑
            }
        },
        mounted () {
            let Height = document.getElementsByClassName('single-page-con')[0].offsetHeight;
            this.$store.dispatch({type: 'calManiViewHeight', payload: Height});
            addEventListener('resize', () => {
                let Height = document.getElementsByClassName('single-page-con')[0].offsetHeight;
                this.$store.dispatch({type: 'calManiViewHeight', payload: Height});
            });
            this.$store.commit('UPDATE_DOCUMENT_HEIGHT', Height);
            this.init();
        },
        created () {
            this.$store.commit('setOpenedList');
        }
    };
</script>
